package com.maybach.core.manager.impl;

import com.maybach.common.model.LoanOrderDO;
import com.maybach.common.query.LoanOrderQuery;
import com.maybach.core.dal.mapper.ext.LoanOrderExtMapper;
import com.maybach.core.manager.LoanOrderManager;
import com.maybach.util.query.support.PageResult;
import org.apache.shiro.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * Manager for LoanOrder.
 */

@Component
public class LoanOrderManagerImpl implements LoanOrderManager{
    protected Logger log = LoggerFactory.getLogger(this.getClass());

    
    @Autowired
    protected LoanOrderExtMapper loanOrderExtMapper;
    /**
     * query count by query condition.
     */
    @Override
    public int countByQuery(LoanOrderQuery query){
        return loanOrderExtMapper.countByQuery(query);
    }

    /**
     * delete by query condition.
     */
    @Override
    public int deleteByQuery(LoanOrderQuery query){
        return loanOrderExtMapper.deleteByQuery(query);
    }

    /**
     * delete by primary key.
     */
    @Override
    public int deleteByPrimaryKey(LoanOrderDO record){
        return loanOrderExtMapper.deleteByPrimaryKey(record);
    }

    /**
     * insert selective.
     */
    @Override
    public long insertSelective(LoanOrderDO record){
        return loanOrderExtMapper.insertSelective(record);
    }

    /**
     * select by query condition.
     */
    @Override
    public List<LoanOrderDO> selectByQuery(LoanOrderQuery query){
        return loanOrderExtMapper.selectByQuery(query);
    }

    /**
     * select by query condition with page.
    */
    @Override
    public PageResult<LoanOrderDO> selectByQueryWithPage(LoanOrderQuery query) {
        PageResult<LoanOrderDO> result = new PageResult<>();
        result.setPageSize(query.getPageSize());
        if(null != query.getPageNo()) {
            result.setPageNo(query.getPageNo());
        }
        result.setTotalCount(this.countByQuery(query));
        result.setResult(this.selectByQuery(query));
        return result;
    }

    /**
     * select by primary key.
     */
    @Override
    public LoanOrderDO selectByPrimaryKey(Long id){
        return loanOrderExtMapper.selectByPrimaryKey(id);
    }

    /**
     * update by query condition selective.
     */
    @Override
    public int updateByQuerySelective( LoanOrderDO record,  LoanOrderQuery query){
        return loanOrderExtMapper.updateByQuerySelective(record,  query);
    }

    /**
     * update by query condition.
     */
    @Override
    public int updateByQuery( LoanOrderDO record,  LoanOrderQuery query){

        return loanOrderExtMapper.updateByQuery(record, query);
    }

    /**
     * update by primary key selective.
     */
    @Override
    public int updateByPrimaryKeySelective(LoanOrderDO record){
        return loanOrderExtMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public LoanOrderDO selectByOutOrderId(Long outOrderId) {
        LoanOrderQuery query = new LoanOrderQuery();
        query.or().andOutOrderIdEqualTo(outOrderId);
        List<LoanOrderDO> list = loanOrderExtMapper.selectByQuery(query);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }
}